package jnpf.scheduletask.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.scheduletask.ScheduletaskApi;
import jnpf.scheduletask.TaskApi;
import jnpf.task.entity.TaskEntity;
import jnpf.task.model.task.TaskConstant;
import jnpf.task.model.task.TaskForm;
import jnpf.task.model.task.TaskPagination;
import jnpf.task.service.TaskService;
import jnpf.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * task
 * @版本： V3.6
 * @版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-11-08
 */
@Slf4j
@RestController
@Tag(name = "task" , description = "JNPF")
@RequestMapping("/Task")
public class TaskController implements TaskApi {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private TaskService taskService;



    /**
    * 列表
    *
    * @param taskPagination
    * @return
    */
    @Operation(summary = "获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody TaskPagination taskPagination)throws IOException{
        List<TaskEntity> list= taskService.getList(taskPagination);
        List<Map<String, Object>> realList=new ArrayList<>();
        for ( TaskEntity entity : list) {
        Map<String, Object> taskMap=JsonUtil.entityToMap(entity);
        taskMap.put("id", taskMap.get("id"));
        //副表数据
        //子表数据
            realList.add(taskMap);
        }
        //数据转换
        boolean isPc = ServletUtil.getHeader("jnpf-origin").equals("pc");
        realList = generaterSwapUtil.swapDataList(realList,    TaskConstant.getFormData(),   TaskConstant.getColumnData(), taskPagination.getModuleId(),isPc?false:false);

        //返回对象
        PageListVO vo = new PageListVO();
        vo.setList(realList);
        PaginationVO page = JsonUtil.getJsonToBean(taskPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }
    /**
    * 创建
    *
    * @param taskForm
    * @return
    */
      @PostMapping()
    @Operation(summary = "创建")
    public ActionResult create(@RequestBody @Valid TaskForm taskForm) {
        String b = taskService.checkForm(taskForm,0);
        if (StringUtil.isNotEmpty(b)){
            return ActionResult.fail(b );
        }
        try{
            taskService.saveOrUpdate(taskForm, null ,true);
        }catch(Exception e){
            e.printStackTrace();
            return ActionResult.fail("新增数据失败");
        }
        return ActionResult.success("创建成功");
    }
    /**
    * 编辑
    * @param id
    * @param taskForm
    * @return
    */
    @PutMapping("/{id}")
    @Operation(summary = "更新")
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid  TaskForm taskForm,
                                @RequestParam(value = "isImport", required = false) boolean isImport){
        if (!isImport) {
            String b =  taskService.checkForm(taskForm,1);
            if (StringUtil.isNotEmpty(b)){
                return ActionResult.fail(b );
            }
        }
        TaskEntity entity= taskService.getInfo(id);
        if(entity!=null){
            try{
                taskService.saveOrUpdate(taskForm,id,false);
            }catch(Exception e){
                return ActionResult.fail("修改数据失败");
            }
            return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }
     /**
    * 删除
    * @param id
    * @return
    */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        TaskEntity entity= taskService.getInfo(id);
        if(entity!=null){
            //主表数据删除
            taskService.delete(entity);
        }
        return ActionResult.success("删除成功");
    }
    /**
    * 表单信息(详情页)
    * 详情页面使用-转换数据
    * @param id
    * @return
    */
    @Operation(summary = "表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult detailInfo(@PathVariable("id") String id){
        TaskEntity entity= taskService.getInfo(id);
        if(entity==null){
            return ActionResult.fail("表单数据不存在！");
        }
        Map<String, Object> taskMap=JsonUtil.entityToMap(entity);
        taskMap.put("id", taskMap.get("id"));
        //副表数据
        //子表数据
        boolean isPc = ServletUtil.getHeader("jnpf-origin").equals("pc");
        taskMap = generaterSwapUtil.swapDataDetail(taskMap,  TaskConstant.getFormData(),"624518517466603461",isPc?false:false);
        return ActionResult.success(taskMap);
    }
   /**
    * 获取详情(编辑页)
    * 编辑页面使用-不转换数据
    * @param id
    * @return
    */
    @Operation(summary = "信息")
    @GetMapping("/{id}")
    public ActionResult info(@PathVariable("id") String id){
        TaskEntity entity= taskService.getInfo(id);
        if(entity==null){
            return ActionResult.fail("表单数据不存在！");
        }
        Map<String, Object> taskMap=JsonUtil.entityToMap(entity);
        taskMap.put("id", taskMap.get("id"));
        //副表数据
        //子表数据
        taskMap = generaterSwapUtil.swapDataForm(taskMap,  TaskConstant.getFormData(),  TaskConstant.TABLEFIELDKEY,  TaskConstant.TABLERENAMES);
        return ActionResult.success(taskMap);
    }

}
